import { getuser } from '../../utils/storage'
import { procallback } from '../../utils/index'
import { request, wxlogin } from '../../utils/https'
import * as moment from 'moment'
import { getschoolclazzstudentcalendarbyid, changeschoolclazzstudentcalendar } from '../../utils/interfaces'

export default {
  data () {
    return {
      btntitle: '请假',
      startqs: false,
      isshow: false,
      schooladdress: '',
      image: '',
      announcement: {
        ID: '',
        hour: '',
        info: '',
        time: '',
        schoolclassroom: {
          name: ''
        },
        schoolclasstime: '',
        schoolclazz: {
          name: ''
        },
        schoolclazzstudentcalendar: null,
        schooluser: {
          name: ''
        },
        username: '',
        start: ''
      }
    }
  },
  async onLoad (option) {
    this.isshow = false
    this.startqs = false
    const time = new Date().getTime()
    this.btntitle = '请假'
    const { ID } = await getuser('user')
    // console.log(schooladdress)
    // this.schooladdress = schooladdress.name
    try {
      let code = await wxlogin()
      const data = await request(getschoolclazzstudentcalendarbyid, {
        data: {
          schoolstudentid: ID,
          code,
          ID: option.ID
        }
      })
      data.time = moment(data.starttime).format('YYYY年MM月DD日')
      this.start = moment(data.starttime).format('MM月DD日')
      const starttime = new Date(data.starttime).getTime()
      if (data.schoolclazzstudentcalendar.length) {
        const schoolclazzstudentcalendar = data.schoolclazzstudentcalendar[0]
        if (schoolclazzstudentcalendar.type === '请假') {
          if (time < starttime) {
            this.start = ''
            this.isshow = true
            this.btntitle = '取消请假'
          }
        }
        let title = ''
        let images = []
        if (schoolclazzstudentcalendar.data) {
          title = schoolclazzstudentcalendar.data.title ? schoolclazzstudentcalendar.data.title : ''
          images = schoolclazzstudentcalendar.data.images ? schoolclazzstudentcalendar.data.images : []
        }
        schoolclazzstudentcalendar.data = {
          title,
          images
        }
        data.schoolclazzstudentcalendar = schoolclazzstudentcalendar
      } else {
        data.schoolclazzstudentcalendar = null
      }
      this.announcement = data
    } catch (err) {
      wx.showModal({
        title: '提示',
        content: '服务器请求失败，点击确定重新加载',
        success: res => {
          if (res.confirm) {
            this.start()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  computed: {
    calendar () {
      const { announcement } = this
      return announcement.schoolclazzstudentcalendar || false
    }
  },
  methods: {
    showimage (current) {
      wx.previewImage({
        urls: this.announcement.schoolclazzstudentcalendar.data.images,
        current
      })
    },
    async handleOpen2 () {
      const { start, btntitle } = this
      const { error } = await procallback(wx.showModal, {
        title: '提示',
        content: `您确定在${start}为孩子${btntitle}吗？`
      })
      console.log(error)
      if (error) {
        this.handleClose2()
      }
    },
    async handleClose2 () {
      const { ID: schoolclazzscheduleid } = this.announcement
      const { ID: schoolstudentid } = await getuser('user')
      let code = await wxlogin()
      await request(changeschoolclazzstudentcalendar, {
        data: {
          code,
          schoolclazzscheduleid,
          schoolstudentid
        }
      })
      wx.switchTab({
        url: '/pages/index/main'
      })
    }
  }
}
